package com.zzr.demo.redis.controller;

import com.zzr.demo.redis.service.CounterService;
import com.zzr.demo.redis.service.LogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: xiaomi
 * @date: 2021/10/8
 * @description: 限速器的应用：一段时间内，密码输入错误的次数。
 * 还可以应用于爬虫次数的限制。
 */
@RestController
@RequestMapping("speed/limiter")
@Api(tags = "限速器：应用：1分钟内密码输入错误次数")
@Slf4j
public class LoggerErrorLimiterController {
    @Autowired
    @Qualifier("SpeedLimiter")
    CounterService counterService;

    @GetMapping("/init")
    @ApiOperation("初始化限速器")
    public boolean init(int value) {
        counterService.init(value);
        return true;
    }

    @GetMapping("/logError")
    @ApiOperation("登录错误")
    public String execute() {
        long decrease = counterService.decrease();
        if (decrease < 0) {
            return "登录错误次数太多，账号已被锁定！";
        }
        return "只能再输错 " + decrease + " 次！";
    }


}
